import { Component, OnInit } from '@angular/core';
import {AuthService} from "@core/service/AuthService";
import {Router} from "@angular/router";

import { NzNotificationService } from 'ng-zorro-antd/notification';
import {BBYUtils} from "@shared/utils/BBYUtils";
interface UserInfoView {
  "birthday": string,
  "signature": string,
  "storageUse": string,
  "storageAll": string,
  "labelNum": number,
  "sex": string,
  "userId": string,
  "scores": number,
  "beCollected": number
  "email": string,
  "username": string,
  "storageUsePercent":number
}
@Component({
  selector: 'bby-personal',
  templateUrl: './personal.component.html',
  styleUrls: ['./personal.component.scss']
})
export class PersonalComponent implements OnInit {

  constructor(
    public authService:AuthService,
    private router: Router,
    private notification: NzNotificationService
  ) {
    let userInfo = authService.getUserInfo();
    if (userInfo){
      this.userInfo = {
        birthday: userInfo.birthday,
        signature:userInfo.signature,
        storageUse:this.transByte(userInfo.storageUse),
        storageAll:this.transByte(userInfo.storageByte),
        labelNum:userInfo.labelNum,
        sex:userInfo.sex,
        userId:userInfo.userId,
        scores:userInfo.scores,
        beCollected:userInfo.beCollected,
        email:userInfo.email,
        username:userInfo.username,
        storageUsePercent:userInfo.storageUse/userInfo.storageByte*100
      }
    }

  }
  userInfo: UserInfoView | undefined ;

  editor="";
  editInput="";
  ngOnInit(): void {


  }
  transByte(byte:number){
    if (byte<1073741824) return (byte/1048576).toFixed(2)+'M';
    else if (byte<1099511627776) return byte/1073741824+'G';
    else return byte/1099511627776+'T';
  }
  toastMessage:any;
  inputFocusout(){
    this.editor='';
  }
  logout(){
    this.authService.logout();
    this.toastMessage=[
      {
        severity: 'success',
        summary: '已经注销'
      }
    ];
    setTimeout( ()=> {
      this.router.navigate(['/']);
    },1000)
  }
  createAutoUpdatingNotifications(): void {
    this.notification.create('error','Notification Title', 'Description.');
  }

  signatureChange() {
    this.authService.uploadCustom('signature',this.authService.userInfo?.signature||'')
  }

  openSource() {
    window.open("https://gitee.com/ben-yin/dashboard/projects")
  }
}
